import diabind
import sys

_PRINTABLE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"

def dumpdebugstreams(filename):
    if filename[-4:].lower() == ".pdb":
        enumstreams = diabind.load_pdb(filename).getEnumDebugStreams()
    else:
        enumstreams = diabind.load_exe(filename, "").getEnumDebugStreams()

    for stream_index in range( len(enumstreams) ):
        stream = enumstreams[stream_index]
        print( "" )
        print( "stream#{} {}".format(stream_index, stream.get_name()) )

        for data_index in range( len(stream) ):
            data = stream[data_index]

            print( "" )
            print( "\tdata#{}, {} bytes".format(data_index, len(data)) )

            formated_hex = ""
            formated_string = ""
            for i in range( len(data) ):
                if formated_hex and (i % 16) == 0:
                    print( "\t{:48} : {}".format(formated_hex, formated_string) )
                    formated_hex = ""
                    formated_string = ""

                formated_hex += " {:02x}".format( ord(data[i]) )
                formated_string += data[i] if data[i] in _PRINTABLE else "."

            print( "\t{:48} : {}".format(formated_hex, formated_string) )

if __name__ == '__main__':
    if len(sys.argv) != 2:
        if len(sys.argv) != 1:
            print( "invalid parameters" )
        print( "usage {} <pdb_or_pe_file>".format(sys.argv[0]) )
        exit(1)

    try:
        dumpdebugstreams(sys.argv[1])
    except diabind.DiaError as err:
        print( str(err) )
